﻿using System;
using System.Data;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!String.IsNullOrEmpty(DAL.Common.isLogin(Session["UserId"])))
            {
                Response.Redirect("~/Member/Default.aspx", true);
            }
            else
            {
                Session.Remove("UserId");
            }
           

        }
    }
    protected void login_in_sumbit_Click(object sender, EventArgs e)
    {
        #region 基础判断

        if (txtMobile.Text.Trim() == string.Empty)
        {
            errorTip.Text = "登录名或密码错误";
            return;
        }
        if (txtPassword.Text.Trim() == string.Empty)
        {
            errorTip.Text = "登录名或密码错误";
            return;
        }
        if (Request.Cookies["CheckCode"].Value != TxtCode.Text.ToUpper())
        {
            errorTip.Text = "验证码错误！";
            return;
        }
        if (BLL.User.CheckMemberIsFreeState(txtMobile.Text.Trim()))
        {
            CommenMethod.GetJavaScript("该账号已冻结，请联系管理员！");
            return;
        }
        int status = 0;
        DataTable dtConfig = BLL.Config.GetTableConfig();
        status = Convert.ToInt32(dtConfig.Rows[0]["WebClose"]);
        if (status == 1)
        {
            CommenMethod.GetJavaScript("网站正在维护，暂时无法登陆！");
            return;
        }

        //判断用户名或者手机号是否正确
        if (CommenMethod.IsMobile(txtMobile.Text.Trim()))
        {
            //判断手机号是否存在
            bool tbuser = BLL.User.IsExistsMobile(txtMobile.Text.Trim());
            if (tbuser)
            {
                //CommenMethod.GetJavaScript("手机号已注册，请更换手机号!");
                //return;
                if (!BLL.User.IsExistMemberMobile(txtMobile.Text.Trim(), CommenMethod.md5(txtPassword.Text.Trim(), 32)))
                {
                    errorTip.Text = "登录名或密码错误";
                    return;
                }
                //根据手机号查询用户名
                DataTable tbusermobile = BLL.User.GetMemberListPage("select * from [user] where  Mobile='" + txtMobile.Text + "'");
                if (tbusermobile.Rows.Count > 0)
                {
                    txtMobile.Text = tbusermobile.Rows[0]["Userid"].ToString();
                }
                else
                {
                    errorTip.Text = "不存在该用户";
                    return;
                }
            }
            else
            {
                errorTip.Text = "不存在该手机号";
                return;
            }
        }
        else
        {
            if (!BLL.User.IsExistMember(txtMobile.Text.Trim(), CommenMethod.md5(txtPassword.Text.Trim(), 32)))
            {
                errorTip.Text = "登录名或密码错误";
                return;
            }
        }


        if (BLL.User.CheckMemberIsFreeState(txtMobile.Text.Trim()))
        {
            CommenMethod.GetJavaScript("该账号已冻结，请联系管理员！");
            return;
        }
        //DataTable dtUserInfo = BLL.User.GetReferee(txtMobile.Text);
        //if (Int32.Parse(dtUserInfo.Rows[0]["FrozenState"].ToString()) == 0)
        //{
        //    CommenMethod.GetJavaScript("账号还未激活，暂时无法登录系统!");
        //    return;
        //}
        #endregion

        string ip = Page.Request.UserHostAddress;
        // when username and password is correct 
        DataTable dtUserInfo = BLL.User.GetReferee(txtMobile.Text);
        if (dtUserInfo.Rows.Count > 0)
        {
            Session.Add("UserId", dtUserInfo.Rows[0]["userid"].ToString());
            Session.Add("Name", dtUserInfo.Rows[0]["userid"].ToString());
        }
        Response.Redirect("~/Member/Default.aspx", true);
    }

    /// <summary>
    /// 忘记密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbngetpass_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtMobile.Text.Trim()))
        {
            CommenMethod.GetJavaScript("请先输入会员账号！", "login.aspx");
            return;
        }
        Response.Redirect("FindPassword.aspx?userId=" + txtMobile.Text.Trim());
        Response.End();
    }
    //
    /// <summary>
    /// 会员注册
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Reg.aspx");
        Response.End();
    }
    public bool SendEmail(string receiver, string neirong)
    {
        //System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();  
        //邮件发送时请确认服务的杀毒软件因素

        System.Net.Mail.SmtpClient client;
        client = new System.Net.Mail.SmtpClient("smtp.163.com");
        client.Timeout = 60000;

        //client.UseDefaultCredentials = false;

        client.UseDefaultCredentials = true;
        client.Credentials = new System.Net.NetworkCredential("passwd888@163.com", "fyh778899");
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.From = new System.Net.Mail.MailAddress("passwd888@163.com", "公司", System.Text.Encoding.UTF8);
        message.To.Add(new System.Net.Mail.MailAddress(receiver, "正式会员", System.Text.Encoding.UTF8));
        message.IsBodyHtml = false;
        message.Subject = "密码初始化";
        message.Body = neirong;
        try
        {
            client.Send(message);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 会员注册
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void reg_in_sumbit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Reg.aspx");
        Response.End();
    }
}
